#!/usr/bin/env python
import wx
import os
import wbfm_wav_nogui_1 as wbfm

class MyFrame(wx.Frame):
	def __init__(self,parent,title):
		wx.Frame.__init__(self,parent,title=title,size=(-1,-1))
		#self.control = wx.TextCtrl(self,style=wx.TE_MULTILINE)
		self.CreateStatusBar()
		#filemenu = wx.Menu()
		panel = wx.Panel(self,-1)

		self.textCH1 = wx.TextCtrl(panel,value = '/home/adam/hackRfProj/kalimaba.wav')
		self.btnCH1 = wx.Button(panel,label = 'Browser')
		ch1 = wx.BoxSizer(wx.HORIZONTAL)
		ch1.Add(wx.StaticText(panel,label = 'CH1 File: '),0,wx.CENTER)
		ch1.Add(self.textCH1,1,wx.CENTER)
		ch1.Add(self.btnCH1,0,wx.CENTER)

		self.textCH2 = wx.TextCtrl(panel,value = '/home/adam/hackRfProj/test.wav')
		self.btnCH2 = wx.Button(panel,label = 'Browser')
		ch2 = wx.BoxSizer(wx.HORIZONTAL)
		ch2.Add(wx.StaticText(panel,label = 'CH2 File: '),0,wx.CENTER)
		ch2.Add(self.textCH2,1,wx.CENTER)
		ch2.Add(self.btnCH2,0,wx.CENTER)

		self.sliderAm1 = wx.Slider(panel,-1,minValue = 1,maxValue = 15,style=wx.SL_HORIZONTAL)
		am1 = wx.BoxSizer(wx.HORIZONTAL)
		am1.Add(wx.StaticText(panel,label = 'Audio Source: '),0,wx.CENTER)
		am1.Add(self.sliderAm1,1,wx.CENTER)
		
		self.sliderAm2 = wx.Slider(panel,-1,minValue = 0,maxValue = 10,style=wx.SL_HORIZONTAL)
		am2 = wx.BoxSizer(wx.HORIZONTAL)
		am2.Add(wx.StaticText(panel,label = 'CH1'),0,wx.CENTER)
		am2.Add(self.sliderAm2,1,wx.CENTER)
		
		self.sliderAm3 = wx.Slider(panel,-1,minValue = 0,maxValue = 10,style=wx.SL_HORIZONTAL)
		am3 = wx.BoxSizer(wx.HORIZONTAL)
		am3.Add(wx.StaticText(panel,label = 'CH2'),0,wx.CENTER)
		am3.Add(self.sliderAm3,1,wx.CENTER)


		self.textFreq = wx.TextCtrl(panel,value = '101500000')
		freq = wx.BoxSizer(wx.HORIZONTAL)
		freq.Add(wx.StaticText(panel,label = 'Freq'),0,wx.CENTER)
		freq.Add(self.textFreq,0,wx.CENTER)


		self.btnStart = wx.Button(panel,label = 'Start')
		self.btnStop = wx.Button(panel,label = 'Stop')
		self.btnStart.SetDefault()
		self.btnStop.Disable()

		self.Bind(wx.EVT_SLIDER,self.onAm1,self.sliderAm1)
		self.Bind(wx.EVT_SLIDER,self.onAm2,self.sliderAm2)
		self.Bind(wx.EVT_SLIDER,self.onAm3,self.sliderAm3)
		self.Bind(wx.EVT_BUTTON,self.onStart,self.btnStart)
		self.Bind(wx.EVT_BUTTON,self.onStop,self.btnStop)
		self.Bind(wx.EVT_BUTTON,self.onBrowCH1,self.btnCH1)
		self.Bind(wx.EVT_BUTTON,self.onBrowCH2,self.btnCH2)

		btnSizer = wx.BoxSizer(wx.HORIZONTAL)
		self.sizer = wx.BoxSizer(wx.VERTICAL)

		btnSizer.Add(self.btnStart,1,wx.EXPAND)
		btnSizer.Add(self.btnStop,1,wx.EXPAND)

		self.sizer.Add(am1,1,wx.EXPAND)
		self.sizer.Add(ch1,1,wx.EXPAND)
		self.sizer.Add(am2,1,wx.EXPAND)
		self.sizer.Add(ch2,1,wx.EXPAND)
		self.sizer.Add(am3,1,wx.EXPAND)
		self.sizer.Add(freq,1,wx.EXPAND)
		self.sizer.Add(btnSizer,1,wx.EXPAND)
		

		panel.SetSizer(self.sizer)
		panel.SetAutoLayout(1)

		self.sizer.Fit(panel)
		self.Show()

	def onAm1(self,e):
		self.tb.set_am1(e.GetInt())
	def onAm2(self,e):
		self.tb.set_am2(e.GetInt()/10.0)
	def onAm3(self,e):
		self.tb.set_am3(e.GetInt()/10.0)

	def onStart(self,e):
		self.tb = wbfm.wbfm_wav_nogui()
		self.tb.set_freq(int(self.textFreq.GetValue()))
		self.tb.set_CH1(self.textCH1.GetValue())
		self.tb.start()
		self.btnStart.Disable()
		self.btnStop.Enable()
		self.btnCH1.Disable()
		self.btnCH2.Disable()
		self.textCH1.Disable()
		self.textCH2.Disable()
		self.textFreq.Disable()

		
	def onStop(self,e):
		self.tb.stop()
		self.tb.wait()
		self.btnStart.Enable()
		self.btnStop.Disable()
		self.btnCH1.Enable()
		self.btnCH2.Enable()
		self.textCH1.Enable()
		self.textCH2.Enable()
		self.textFreq.Enable()
		
	def onBrowCH1(self,e):
		self.dirname = ''
		dlg = wx.FileDialog(self,"Choose a wav file",self.dirname,"","*.wav",wx.OPEN)
		if dlg.ShowModal() == wx.ID_OK:
			self.filename = dlg.GetFilename()
			self.dirname = dlg.GetDirectory()
			self.textCH1.SetValue(os.path.join(self.dirname,self.filename))
		dlg.Destory()
	def onBrowCH2(self,e):
		self.dirname = ''
		dlg = wx.FileDialog(self,"Choose a wav file",self.dirname,"","*.wav",wx.OPEN)
		if dlg.ShowModal() == wx.ID_OK:
			self.filename = dlg.GetFilename()
			self.dirname = dlg.GetDirectory()
			self.textCH2.SetValue(os.path.join(self.dirname,self.filename))
		dlg.Destory()

	

if __name__ == '__main__':
	app = wx.App(False)
	frame = MyFrame(None,'WBFM Transmitter')
	app.MainLoop()

